package com.wanban.adutils.advertisers.admob

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.AdUtils
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants


class AdmobBannerAdUtils: BaseAdLoader() {

    private var adView: AdView? = null

    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        adView = AdView(activity)
        adView?.setAdSize(AdSize.FULL_BANNER)
        adView?.adUnitId = realUnitId
        val adRequest = AdUtils.getAdRequest()
        adView?.adListener = object: AdListener() {

            override fun onAdLoaded() {
                super.onAdLoaded()
                if (isTimeout) {
                    destroy()
                    return
                }
                setAdLoadSuccess()
                sdkLoadFinish()
                callback.onAdLoaded(adView)
            }

            override fun onAdFailedToLoad(errorCode : LoadAdError) {
                super.onAdFailedToLoad(errorCode)
                if (isTimeout) {
                    destroy()
                    return
                }
                sdkLoadFinish()
                callback.onAdLoadFailed(errorCode.code)
                destroy()
                val code = getAdmobErrorCode(errorCode)
                analysisErrorCode(adInfo, code)
            }

            override fun onAdClosed() {
                super.onAdClosed()
                callback.onAdComplete()
            }

            override fun onAdClicked() { // admob点击相当于完成
                super.onAdClicked()
                callback.onAdClicked()
            }
        }
        adView?.loadAd(adRequest)
    }


//    override fun loadAd(activity: Activity?, adInfo: AdInfo, callback: OnLoadAdCallback)  {
//        super.loadAd(activity, adInfo, callback)
//        if (activity == null || activity.isFinishing) {
//            callback.onAdLoadFailed(AdConstants.ERROR_CODE_ACTIVITY_DESTROYED)
//            return
//        }
//        adView = AdView(activity)
//        adView?.adUnitId = realUnitId
//        val adRequest = AdUtils.getAdRequest()
//        adView?.adListener = object: AdListener() {
//
//            override fun onAdLoaded() {
//                super.onAdLoaded()
//                sdkLoadFinish()
//                callback.onAdLoaded(adView)
//            }
//
//            override fun onAdFailedToLoad(errorCode : LoadAdError) {
//                super.onAdFailedToLoad(errorCode)
//                val code = getAdmobErrorCode(errorCode)
//                analysisErrorCode(adInfo, code)
//                sdkLoadFinish()
//                callback.onAdLoadFailed(errorCode.code)
//                destroy()
//            }
//
//            override fun onAdClosed() {
//                super.onAdClosed()
//                callback.onAdComplete()
//            }
//
//            override fun onAdClicked() { // admob点击相当于完成
//                super.onAdClicked()
//                callback.onAdComplete()
//            }
//
//        }
//        adView?.loadAd(adRequest)
//    }


    override fun showAd(adRoot: ViewGroup, type: Int) {
        if (adView == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_INVALID_REQUEST)
            destroy()
            return
        }
        adRoot.removeAllViews()
        adRoot.visibility = View.VISIBLE
        adRoot.addView(adView)
    }

    override fun destroy(){
        super.destroy()
        if (adView == null) return
        val adRoot = adView?.parent as ViewGroup?
        if (adRoot != null) {
            adRoot.removeAllViews()
            adRoot.visibility = View.GONE
        }
        adView?.destroy()
        adView = null
    }


    private fun getAdSize(): AdSize? { // Step 2 - Determine the screen width (less decorations) to use for the ad width.
        val widthPixels = AdUtils.appContext.resources.displayMetrics.widthPixels
        val density = AdUtils.appContext.resources.displayMetrics.density
        val adWidth = (widthPixels / density).toInt()
        // Step 3 - Get adaptive ad size and return for setting on the ad view.
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
                AdUtils.appContext,
                adWidth
        )
    }
}